Task 1

Image compression

Submitted to: Dr. Michael Melek

_________________________________________________

Supervisor: Dr. Michael Melek
Author: Ahmad Kamal Hamdy
Date: December 2022
Team
Ahmad Awad Sec: 1 Bn: 20
Ahmad Kamal Sec: 1 Bn: 21
The code is written using matlab online live script to be able to export the code with the outputs in the same logical flow used to get each of the requirements.
Most of the variables are written using clean code techniques to make the code self explainable
Resources: matlab help, forum and documentation in addition to several youtube videos
Note: To get the size of the compressed images, I came up with a new technique inwhich I use blocstruct to make a compressing matrix, inwhich it's size vary according to the value of m. By using this technique I did not need to use any for loops, or any loops in general, and the results are similar to the technique that uses for loops
For any questions concerning image compression code, I would be glad to be contacted at my personal gmail
%first two lines reads and displays the image
original_image = imread('image1.bmp');
imshow(original_image)
title("Original image")
original_image_dimensions=size(original_image)
original_image_dimensions = 1×3
1080 1920 3
info = imfinfo('image1.bmp');
fprintf('The size of original image on disk is <strong>%d bytes</strong>.\n', ...
info.FileSize);
The size of original image on disk is 6220854 bytes.
original_image_in_double = im2double(original_image);

We will get the three components of the original image (R,G,B), display them and we will mention their size on disk in addition to commenting on the results and labeling the figures

%These two lines creates a new matrix for the red component and displays it
original_image_red_component = original_image(:,:,1);
imshow(original_image_red_component)
title("Red component of original image ",'Color','r')
red_component_dimensions = size(original_image_red_component)
red_component_dimensions = 1×2
1080 1920
imwrite(original_image_red_component,'red_component.bmp');
red_component_info = imfinfo("red_component.bmp");
fprintf(['The size of red component on disk is<strong> %d bytes</strong>, which is' ...
' one third the original image size\n\n'], red_component_info.FileSize);
The size of red component on disk is 2074678 bytes, which is one third the original image size
fprintf(['The sky appears to be dark in the red component as the sky has mostly' ...
' blue color\n']);
The sky appears to be dark in the red component as the sky has mostly blue color
fprintf(['We can deduce that the dark areas has less red component and the bright' ...
' areas has little red components such as the reflection of the sun on ' ...
'the snow\n']);
We can deduce that the dark areas has less red component and the bright areas has little red components such as the reflection of the sun on the snow
 
%These two lines creates a new matrix for the green component and displays it
original_image_green_component = original_image(:,:,2);
imshow(original_image_green_component)
title("Green component of original image ",'Color','g')
green_component_dimensions = size(original_image_green_component)
green_component_dimensions = 1×2
1080 1920
imwrite(original_image_green_component,'green_component.bmp');
green_component_info = imfinfo("green_component.bmp");
fprintf(['The size of green_component on disk is<strong> %d bytes</strong>, which' ...
' is one third the original image size\n\n'], green_component_info.FileSize);
The size of green_component on disk is 2074678 bytes, which is one third the original image size
fprintf(['The trees appears to be bright in the green component as they have green' ...
' color']);
The trees appears to be bright in the green component as they have green color
 
%These two lines creates a new matrix a new matrix for the blue component and
% displays it
original_image_blue_component = original_image(:,:,3);
imshow(original_image_blue_component)
title("Blue component of original image ",'Color','b')
blue_component_dimensions = size(original_image_blue_component)
blue_component_dimensions = 1×2
1080 1920
imwrite(original_image_blue_component,'blue_component.bmp');
blue_component_info = imfinfo("blue_component.bmp");
fprintf(['The size of blue_component on disk is <strong>%d bytes</strong>, which is' ...
' one third the original image size\n\n'], blue_component_info.FileSize);
The size of blue_component on disk is 2074678 bytes, which is one third the original image size
fprintf(['The sky appears bright in the blue component as the color of the sky in' ...
' the original image is mostly blue'])
The sky appears bright in the blue component as the color of the sky in the original image is mostly blue
 

To get the 2d dct at m = 1

psnr_decompressed_original_component = [0; 0; 0; 0;];
compressing_mat_at_m_equals_one = [1 0 0 0 0 0 0 0 ];
comp= @(block_struct) compressing_mat_at_m_equals_one* block_struct.data * ...
compressing_mat_at_m_equals_one';
 
mask_at_m_equals_one = [1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
 
 
% for the red component at m = 1
red_component_in_double = im2double(original_image_red_component);
dct_red_component = dctmtx(8);
 
dct = @(block_struct) dct_red_component* block_struct.data * dct_red_component';
B_R1 = blockproc(red_component_in_double,[8 8],dct);
B2_R1 = blockproc(B_R1,[8 8],@(block_struct) mask_at_m_equals_one .* ...
block_struct.data);
comp_R1 = blockproc(B2_R1,[8 8],comp);
imwrite(comp_R1,'red_component_at_m_equals_one.bmp');
red_component_at_m_equals_one_info = imfinfo("red_component_at_m_equals_one.bmp");
fprintf(['The size of compressed red component at m = 1 on disk is ' ...
'<strong>%d bytes</strong>, which is too small compared to the' ...
' red component of the original image\n\n'], ...
red_component_at_m_equals_one_info.FileSize);
The size of compressed red component at m = 1 on disk is 33478 bytes, which is too small compared to the red component of the original image
 
%inverse 2-d dct to get the decompressed image of the red component at m = 1
invdct = @(block_struct) dct_red_component' * block_struct.data * ...
dct_red_component;
R1 = blockproc(B2_R1,[8 8],invdct);
figure
imshow(R1)
title("Decompressed red component at m = 1 ",'Color','r')
xlabel('The size of the compressed red component at m = 1 equals 33478 bytes ')
psnr_decompressed_red_component_at_m_equals_one = psnr(R1,red_component_in_double)
psnr_decompressed_red_component_at_m_equals_one = 19.8695
 
 
% for the green component at m = 1
green_component_in_double = im2double(original_image_green_component);
dct_green_component = dctmtx(8);
 
dct = @(block_struct) dct_green_component* block_struct.data *...
dct_green_component';
B_G1 = blockproc(green_component_in_double,[8 8],dct);
B2_G1 = blockproc(B_G1,[8 8],@(block_struct) ...
mask_at_m_equals_one .* block_struct.data);
comp_G1 = blockproc(B2_G1,[8 8],comp);
imwrite(comp_G1,'green_component_at_m_equals_one.bmp');
green_component_at_m_equals_one_info = imfinfo("green_component_at_m_equals" + ...
"_one.bmp");
fprintf(['The size of compressed green component at m = 1 on disk is' ...
' <strong>%d bytes</strong>, which is too small compared to the' ...
' green component of the original image\n\n'], ...
green_component_at_m_equals_one_info.FileSize);
The size of compressed green component at m = 1 on disk is 33478 bytes, which is too small compared to the green component of the original image
 
%inverse 2-d dct to get the decompressed image of the green component at m = 1
invdct = @(block_struct) dct_green_component' * ...
block_struct.data* dct_green_component;
G1 = blockproc(B2_G1,[8 8],invdct);
figure
imshow(G1)
title("Decompressed green component at m = 1 ",'Color','g')
xlabel('The size of the compressed red component at m = 1 equals 33478 bytes ')
psnr_decompressed_green_component_at_m_equals_one = ...
psnr(G1,green_component_in_double)
psnr_decompressed_green_component_at_m_equals_one = 19.7613
 
% for the blue component at m = 1
blue_component_in_double = im2double(original_image_blue_component);
dct_blue_component = dctmtx(8);
 
dct = @(block_struct) dct_blue_component* block_struct.data * dct_blue_component';
B_B1 = blockproc(blue_component_in_double,[8 8],dct);
B2_B1 = blockproc(B_B1,[8 8],@(block_struct) ...
mask_at_m_equals_one .* block_struct.data);
comp_B1 = blockproc(B2_B1,[8 8],comp);
imwrite(comp_B1,'blue_component_at_m_equals_one.bmp');
blue_component_at_m_equals_one_info = imfinfo("blue_component_at_m_equals_one.bmp");
fprintf(['The size of compressed blue component at m = 1 on disk is' ...
' <strong>%d bytes</strong>, which is too small compared to the ' ...
' blue component of the original image\n\n'], ...
blue_component_at_m_equals_one_info.FileSize);
The size of compressed blue component at m = 1 on disk is 33478 bytes, which is too small compared to the blue component of the original image
 
%inverse 2-d dct to get the decompressed image of the blue component at m = 1
invdct = @(block_struct) dct_blue_component' * block_struct.data * dct_blue_component;
B1 = blockproc(B2_B1,[8 8],invdct);
figure
imshow(B1)
title("Decompressed blue component at m = 1 ",'Color','b')
xlabel('The size of the compressed blue component at m = 1 equals 33478 bytes ')
psnr_decompressed_blue_component_at_m_equals_one = ...
psnr(B1,blue_component_in_double)
psnr_decompressed_blue_component_at_m_equals_one = 19.9629
 
 
%To get the colored image at m=1
comp_RGB1 = cat(3,comp_R1,comp_G1,comp_B1);
imwrite(comp_RGB1,'comp_original_component_at_m_equals_one.bmp');
compressed_original_component_at_m_equals_one_info = imfinfo("comp_original" + ...
"_component_at_m_equals_one.bmp");
fprintf(['The size of compressed original component at m = 1 on ' ...
'disk is <strong>%d bytes</strong>,' ...
' which is too small compared to the size of' ...
' the original image\n\n'], ...
compressed_original_component_at_m_equals_one_info.FileSize)
The size of compressed original component at m = 1 on disk is 97254 bytes, which is too small compared to the size of the original image
RGB1 = cat(3,R1,G1,B1);
imshow(RGB1)
title("Decompressed rgb image at m = 1 ",'Color','k')
xlabel(['The size of the compressed orignal component at m = 1 equals ' ...
'97254 bytes '])
dimensions_of_RGB1 = size(RGB1)
dimensions_of_RGB1 = 1×3
1080 1920 3
fprintf(['We notice that the dimensions of the decompressed images is the same' ...
' as the original image']);
We notice that the dimensions of the decompressed images is the same as the original image
psnr_decompressed_original_component(1) = psnr(RGB1,original_image_in_double)
psnr_decompressed_original_component = 4×1
19.8638 0 0 0

To get the 2d dct at m = 2

compressing_mat_at_m_equals_two = [1 1 0 0 0 0 0 0;
1 1 0 0 0 0 0 0;];
comp= @(block_struct) compressing_mat_at_m_equals_two* block_struct.data * ...
compressing_mat_at_m_equals_two';
 
mask_at_m_equals_two = [1 1 0 0 0 0 0 0
1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
 
 
% for the red component at m = 2
red_component_in_double = im2double(original_image_red_component);
dct_red_component = dctmtx(8);
 
dct = @(block_struct) dct_red_component* block_struct.data * dct_red_component';
B_R2 = blockproc(red_component_in_double,[8 8],dct);
B2_R2 = blockproc(B_R2,[8 8],@(block_struct) mask_at_m_equals_two .* ...
block_struct.data);
comp_R2 = blockproc(B2_R2,[8 8],comp);
imwrite(comp_R2,'red_component_at_m_equals_two.bmp');
red_component_at_m_equals_two_info = imfinfo("red_component_at_m_equals_two.bmp");
fprintf(['The size of compressed red component at m = 2 on disk is ' ...
'<strong>%d bytes</strong>, which is too small compared to the ' ...
' red component of the original image\n\n'], ...
red_component_at_m_equals_two_info.FileSize);
The size of compressed red component at m = 2 on disk is 130678 bytes, which is too small compared to the red component of the original image
 
%inverse 2-d dct to get the decompressed image of the red component at m = 2
invdct = @(block_struct) dct_red_component' * block_struct.data * dct_red_component;
R2 = blockproc(B2_R2,[8 8],invdct);
figure
imshow(R2)
title("Decompressed red component at m = 2 ",'Color','r')
xlabel('The size of the compressed red component at m = 2 equals 130678 bytes ')
psnr_decompressed_red_component_at_m_equals_two = psnr(R2,red_component_in_double)
psnr_decompressed_red_component_at_m_equals_two = 21.9229
 
% for the green component at m = 2
green_component_in_double = im2double(original_image_green_component);
dct_green_component = dctmtx(8);
 
dct = @(block_struct) dct_green_component* block_struct.data * dct_green_component';
B_G2 = blockproc(green_component_in_double,[8 8],dct);
B2_G2 = blockproc(B_G2,[8 8],@(block_struct) mask_at_m_equals_two .* block_struct.data);
comp_G2 = blockproc(B2_G2,[8 8],comp);
imwrite(comp_G2,'green_component_at_m_equals_two.bmp');
green_component_at_m_equals_two_info = imfinfo("green_component_at_m_equals_two.bmp");
fprintf(['The size of compressed green component at m = 2 on disk is ' ...
'<strong>%d bytes</strong>, which is too small compared to the ' ...
'green component of the original image\n\n'], ...
green_component_at_m_equals_two_info.FileSize);
The size of compressed green component at m = 2 on disk is 130678 bytes, which is too small compared to the green component of the original image
 
%inverse 2-d dct to get the decompressed image of the green component at m = 2
invdct = @(block_struct) dct_green_component' * block_struct.data * ...
dct_green_component;
G2 = blockproc(B2_G2,[8 8],invdct);
figure
imshow(G2)
title("Decompressed green component at m = 2 ",'Color','g')
xlabel('The size of the compressed red component at m = 2 equals 130678 bytes ')
psnr_decompressed_green_component_at_m_equals_two = psnr(G2,green_component_in_double)
psnr_decompressed_green_component_at_m_equals_two = 21.8051
 
% for the blue component at m = 2
blue_component_in_double = im2double(original_image_blue_component);
dct_blue_component = dctmtx(8);
 
dct = @(block_struct) dct_blue_component* block_struct.data * dct_blue_component';
B_B2 = blockproc(blue_component_in_double,[8 8],dct);
B2_B2 = blockproc(B_B2,[8 8],@(block_struct) mask_at_m_equals_two .* ...
block_struct.data);
comp_B2 = blockproc(B2_B2,[8 8],comp);
imwrite(comp_B2,'blue_component_at_m_equals_two.bmp');
blue_component_at_m_equals_two_info = imfinfo("blue_component_at_m_" + ...
"equals_two.bmp");
fprintf(['The size of compressed blue component at m = 2 on disk is' ...
'<strong> %d bytes</strong>, which is too small compared to the' ...
' blue component of the original image\n\n'], ...
blue_component_at_m_equals_two_info.FileSize);
The size of compressed blue component at m = 2 on disk is 130678 bytes, which is too small compared to the blue component of the original image
 
%inverse 2-d dct to get the decompressed image of the blue component at m = 2
invdct = @(block_struct) dct_blue_component' * block_struct.data * ...
dct_blue_component;
B2 = blockproc(B2_B2,[8 8],invdct);
figure
imshow(B2)
title("Decompressed blue component at m = 2 ",'Color','b')
xlabel('The size of the compressed blue component at m = 2 equals 130678 bytes ')
psnr_decompressed_blue_component_at_m_equals_two = psnr(B2,blue_component_in_double)
psnr_decompressed_blue_component_at_m_equals_two = 21.9764
 
 
%To get the colored image at m = 2
comp_RGB2 = cat(3,comp_R2,comp_G2,comp_B2);
imwrite(comp_RGB2,'comp_original_component_at_m_equals_two.bmp');
compressed_original_component_at_m_equals_two_info = imfinfo("comp_original_" + ...
"component_at_m_equals_two.bmp");
fprintf(['The size of compressed original component at m = 2 on disk is' ...
' <strong>%d bytes</strong>,' ...
' which is too small compared to the size of the ' ...
'original image\n\n'], ...
compressed_original_component_at_m_equals_two_info.FileSize)
The size of compressed original component at m = 2 on disk is 388854 bytes, which is too small compared to the size of the original image
RGB2 = cat(3,R2,G2,B2);
imshow(RGB2)
title("Decompressed rgb image at m = 2 ",'Color','k')
xlabel(['The size of the compressed orignal component at m = 2 equals ' ...
'388854 bytes '])
dimensions_of_RGB2 = size(RGB2)
dimensions_of_RGB2 = 1×3
1080 1920 3
fprintf(['We notice that the dimensions of the decompressed images is the' ...
' same as the original image']);
We notice that the dimensions of the decompressed images is the same as the original image
psnr_decompressed_original_component(2) = psnr(RGB2,original_image_in_double)
psnr_decompressed_original_component = 4×1
19.8638 21.9009 0 0

To get the 2d dct at m = 3

compressing_mat_at_m_equals_three = [1 1 1 0 0 0 0 0;
1 1 1 0 0 0 0 0;
1 1 1 0 0 0 0 0;];
comp= @(block_struct) compressing_mat_at_m_equals_three* block_struct.data * compressing_mat_at_m_equals_three';
 
mask_at_m_equals_three = [1 1 1 0 0 0 0 0
1 1 1 0 0 0 0 0
1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
% for the red component at m = 3
red_component_in_double = im2double(original_image_red_component);
dct_red_component = dctmtx(8);
 
dct = @(block_struct) dct_red_component* block_struct.data * dct_red_component';
B_R3 = blockproc(red_component_in_double,[8 8],dct);
B2_R3 = blockproc(B_R3,[8 8],@(block_struct) mask_at_m_equals_three .* block_struct.data);
comp_R3 = blockproc(B2_R3,[8 8],comp);
imwrite(comp_R3,'red_component_at_m_equals_three.bmp');
red_component_at_m_equals_three_info = imfinfo("red_component_at_m_equals_three.bmp");
fprintf('The size of compressed red component at m = 3 on disk is <strong>%d bytes</strong>, which is too small compared to the red component of the original image\n\n', red_component_at_m_equals_three_info.FileSize);
The size of compressed red component at m = 3 on disk is 292678 bytes, which is too small compared to the red component of the original image
%inverse 2-d dct to get the decompressed image of the red component at m = 3
invdct = @(block_struct) dct_red_component' * block_struct.data * dct_red_component;
R3 = blockproc(B2_R3,[8 8],invdct);
figure
imshow(R3)
title("Decompressed red component at m = 3 ",'Color','r')
xlabel('The size of the compressed red component at m = 3 equals 292678 bytes ')
psnr_decompressed_red_component_at_m_equals_three = psnr(R3,red_component_in_double)
psnr_decompressed_red_component_at_m_equals_three = 23.7740
 
% for the green component at m = 3
green_component_in_double = im2double(original_image_green_component);
dct_green_component = dctmtx(8);
 
dct = @(block_struct) dct_green_component* ...
block_struct.data * dct_green_component';
B_G3 = blockproc(green_component_in_double,[8 8],dct);
B2_G3 = blockproc(B_G3,[8 8],@(block_struct) ...
mask_at_m_equals_three .* block_struct.data);
comp_G3 = blockproc(B2_G3,[8 8],comp);
imwrite(comp_G3,'green_component_at_m_equals_three.bmp');
green_component_at_m_equals_three_info = imfinfo("green_component_at_m" + ...
"_equals_three.bmp");
fprintf(['The size of compressed green component at m = 3 on ' ...
'disk is <strong>%d bytes</strong>, which is too small compared' ...
' to the green component of the original image\n\n'], ...
green_component_at_m_equals_three_info.FileSize);
The size of compressed green component at m = 3 on disk is 292678 bytes, which is too small compared to the green component of the original image
%inverse 2-d dct to get the decompressed image of the green component at m = 3
invdct = @(block_struct) dct_green_component' * block_struct.data * dct_green_component;
G3 = blockproc(B2_G3,[8 8],invdct);
figure
imshow(G3)
title("Decompressed green component at m = 3 ",'Color','g')
xlabel('The size of the compressed red component at m = 3 equals 292678 bytes ')
psnr_decompressed_green_component_at_m_equals_three = ...
psnr(G3,green_component_in_double)
psnr_decompressed_green_component_at_m_equals_three = 23.6670
 
% for the blue component at m = 3
blue_component_in_double = im2double(original_image_blue_component);
dct_blue_component = dctmtx(8);
 
dct = @(block_struct) dct_blue_component* block_struct.data * dct_blue_component';
B_B3 = blockproc(blue_component_in_double,[8 8],dct);
B2_B3 = blockproc(B_B3,[8 8],@(block_struct) mask_at_m_equals_three .* block_struct.data);
comp_B3 = blockproc(B2_B3,[8 8],comp);
imwrite(comp_B3,'blue_component_at_m_equals_three.bmp');
blue_component_at_m_equals_three_info = imfinfo("blue_component_at_m_equals_three.bmp");
fprintf(['The size of compressed blue component at m = 3 on disk is ' ...
'<strong>%d bytes</strong>, which is too small compared to the' ...
' blue component of the original image\n\n'], ...
blue_component_at_m_equals_three_info.FileSize);
The size of compressed blue component at m = 3 on disk is 292678 bytes, which is too small compared to the blue component of the original image
%inverse 2-d dct to get the decompressed image of the blue component at m = 3
invdct = @(block_struct) dct_blue_component' * block_struct.data ...
* dct_blue_component;
B3 = blockproc(B2_B3,[8 8],invdct);
figure
imshow(B3)
title("Decompressed blue component at m = 3 ",'Color','b')
xlabel('The size of the compressed blue component at m = 3 equals 292678 bytes ')
psnr_decompressed_blue_component_at_m_equals_three = ...
psnr(B3,blue_component_in_double)
psnr_decompressed_blue_component_at_m_equals_three = 23.8248
 
 
%To get the colored image at m=3
comp_RGB3 = cat(3,comp_R3,comp_G3,comp_B3);
imwrite(comp_RGB3,'comp_original_component_at_m_equals_three.bmp');
compressed_original_component_at_m_equals_three_info = imfinfo("comp_original_" + ...
"component_at_m_equals_three.bmp");
fprintf(['The size of compressed original component at m = 3 on disk is' ...
' <strong>%d bytes</strong>,' ...
' which is too small compared to the size of the ' ...
'original image\n\n'], ...
compressed_original_component_at_m_equals_three_info.FileSize)
The size of compressed original component at m = 3 on disk is 874854 bytes, which is too small compared to the size of the original image
RGB3 = cat(3,R3,G3,B3);
imshow(RGB3)
title("Decompressed rgb image at m = 3 ",'Color','k')
xlabel(['The size of the compressed orignal component at m = 3 equals ' ...
'874854 bytes '])
dimensions_of_RGB3 = size(RGB3)
dimensions_of_RGB3 = 1×3
1080 1920 3
fprintf(['We notice that the dimensions of the decompressed ' ...
'images is the same as the original image']);
We notice that the dimensions of the decompressed images is the same as the original image
psnr_decompressed_original_component(3) = ...
psnr(RGB3,original_image_in_double)
psnr_decompressed_original_component = 4×1
19.8638 21.9009 23.7548 0

To get the 2d dct at m = 4

compressing_mat_at_m_equals_four = [1 1 1 1 0 0 0 0;
1 1 1 1 0 0 0 0;
1 1 1 1 0 0 0 0;
1 1 1 1 0 0 0 0];
comp= @(block_struct) compressing_mat_at_m_equals_four* block_struct.data * compressing_mat_at_m_equals_four';
 
mask_at_m_equals_four = [1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
 
 
% for the red component at m = 4
red_component_in_double = im2double(original_image_red_component);
dct_red_component = dctmtx(8);
 
dct = @(block_struct) dct_red_component* block_struct.data * dct_red_component';
B_R4 = blockproc(red_component_in_double,[8 8],dct);
B2_R4 = blockproc(B_R4,[8 8],@(block_struct) ...
mask_at_m_equals_four .* block_struct.data);
comp_R4 = blockproc(B2_R4,[8 8],comp);
imwrite(comp_R4,'red_component_at_m_equals_four.bmp');
red_component_at_m_equals_four_info = imfinfo("red_component_at" + ...
"_m_equals_four.bmp");
fprintf(['The size of compressed red component at m = 4 on disk is' ...
' <strong>%d bytes</strong>, which is too small compared to the' ...
' red component of the original image\n\n'], ...
red_component_at_m_equals_four_info.FileSize);
The size of compressed red component at m = 4 on disk is 519478 bytes, which is too small compared to the red component of the original image
 
%inverse 2-d dct to get the decompressed image of the red component at m = 4
invdct = @(block_struct) dct_red_component' * block_struct.data ...
* dct_red_component;
R4 = blockproc(B2_R4,[8 8],invdct);
figure
imshow(R4)
title("Decompressed red component at m = 4 ",'Color','r')
xlabel('The size of the compressed red component at m = 3 equals 51478 bytes ')
psnr_decompressed_red_component_at_m_equals_four = ...
psnr(R4,red_component_in_double)
psnr_decompressed_red_component_at_m_equals_four = 25.9191
 
% for the green component at m = 4
green_component_in_double = im2double(original_image_green_component);
dct_green_component = dctmtx(8);
 
dct = @(block_struct) dct_green_component* block_struct.data ...
* dct_green_component';
B_G4 = blockproc(green_component_in_double,[8 8],dct);
B2_G4 = blockproc(B_G4,[8 8],@(block_struct) ...
mask_at_m_equals_four .* block_struct.data);
comp_G4 = blockproc(B2_G4,[8 8],comp);
imwrite(comp_G4,'green_component_at_m_equals_four.bmp');
green_component_at_m_equals_four_info = imfinfo("green_component_at" + ...
"_m_equals_four.bmp");
fprintf(['The size of compressed green component at m = 4 on disk is' ...
' <strong>%d bytes</strong>, which is too small compared to the ' ...
' green component of the original image\n\n'], ...
green_component_at_m_equals_four_info.FileSize);
The size of compressed green component at m = 4 on disk is 519478 bytes, which is too small compared to the green component of the original image
%inverse 2-d dct to get the decompressed image of the green component at m = 4
invdct = @(block_struct) dct_green_component' * block_struct.data ...
* dct_green_component;
G4 = blockproc(B2_G4,[8 8],invdct);
figure
imshow(G4)
title("Decompressed green component at m = 4 ",'Color','g')
xlabel('The size of the compressed red component at m = 4 equals 519478 bytes ')
psnr_decompressed_green_component_at_m_equals_four = ...
psnr(G4,green_component_in_double)
psnr_decompressed_green_component_at_m_equals_four = 25.8200
 
% for the blue component at m = 4
blue_component_in_double = im2double(original_image_blue_component);
dct_blue_component = dctmtx(8);
 
dct = @(block_struct) dct_blue_component* block_struct.data * dct_blue_component';
B_B4 = blockproc(blue_component_in_double,[8 8],dct);
B2_B4 = blockproc(B_B4,[8 8],@(block_struct) ...
mask_at_m_equals_four .* block_struct.data);
comp_B4 = blockproc(B2_B4,[8 8],comp);
imwrite(comp_B4,'blue_component_at_m_equals_four.bmp');
blue_component_at_m_equals_four_info = imfinfo("blue_component" + ...
"_at_m_equals_four.bmp");
fprintf(['The size of compressed blue component at m = 4 on disk ' ...
'is <strong>%d bytes</strong>, which is too small compared to' ...
' the blue component of the original image\n\n'], ...
blue_component_at_m_equals_four_info.FileSize);
The size of compressed blue component at m = 4 on disk is 519478 bytes, which is too small compared to the blue component of the original image
 
%inverse 2-d dct to get the decompressed image of the blue component at m = 4
invdct = @(block_struct) dct_blue_component' * ...
block_struct.data * dct_blue_component;
B4 = blockproc(B2_B4,[8 8],invdct);
figure
imshow(B4)
title("Decompressed blue component at m = 4 ",'Color','b')
xlabel('The size of the compressed blue component at m = 4 equals 519478 bytes ')
psnr_decompressed_blue_component_at_m_equals_four = ...
psnr(B4,blue_component_in_double)
psnr_decompressed_blue_component_at_m_equals_four = 25.9715
 
 
%To get the colored image at m=4
comp_RGB4 = cat(3,comp_R4,comp_G4,comp_B4);
imwrite(comp_RGB4,'comp_original_component_at_m_equals_four.bmp');
compressed_original_component_at_m_equals_four_info = imfinfo("comp_original" + ...
"_component_at_m_equals_four.bmp");
fprintf(['The size of compressed original component at m = 4 on' ...
' disk is <strong>%d bytes</strong>,' ...
' which is too small compared to the size of the original image\n\n'], ...
compressed_original_component_at_m_equals_four_info.FileSize)
The size of compressed original component at m = 4 on disk is 1555254 bytes, which is too small compared to the size of the original image
RGB4 = cat(3,R4,G4,B4);
imshow(RGB4)
title("Decompressed rgb image at m = 4 ",'Color','k')
xlabel(['The size of the compressed orignal component at m = 4 equals ' ...
'1555254 bytes '])
dimensions_of_RGB4 = size(RGB4)
dimensions_of_RGB4 = 1×3
1080 1920 3
fprintf(['We notice that the dimensions of the decompressed images' ...
' is the same as the original image']);
We notice that the dimensions of the decompressed images is the same as the original image
psnr_decompressed_original_component(4) = psnr(RGB4,original_image_in_double)
psnr_decompressed_original_component = 4×1
19.8638 21.9009 23.7548 25.9031

Plotting Psnr against m

m_num = [1; 2; 3; 4;];
plot(m_num,psnr_decompressed_original_component,'b-o','LineWidth',4);
xlabel({'m','(at 1,2,3,4)'})
ylabel('PSNR')
title('PSNR against m')